#include "UILayer.h"
#include "common.h"
#include "SimpleAudioEngine.h"

UILayer::UILayer(){
	score = 0;
	hiScore = 10;

}
bool UILayer::init(){
	//load res
	if (!Layer::init())return false;
	//add bg
	auto bg_sprite = Sprite::createWithSpriteFrameName("bg_blank.png");
	this->addChild(bg_sprite);
	bg_sprite->setPosition(Vec2(mysize.width*0.5,mysize.height*0.5));
	
	//add logo
	auto logo_sprite = Sprite::createWithSpriteFrameName("bg_icon.png");
	addChild(logo_sprite);
	logo_sprite->setAnchorPoint(Vec2(0,1));
	logo_sprite->setPosition(Vec2(10,mysize.height-20));


	//add socre
	auto score_sprite = Sprite::createWithSpriteFrameName("bg_score_last.png");
	addChild(score_sprite);
	score_sprite->setTag(TAG_SCORE_BG);
	score_sprite->setAnchorPoint(Vec2(0, 1));
	score_sprite->setPosition(Vec2(240, mysize.height - 20));

	auto label = Label::createWithSystemFont(String::createWithFormat("%d",score)->getCString(),"Consolas",50);
	score_sprite->addChild(label);
	label->setTag(TAG_SCORE);
	label->setPosition(Vec2(score_sprite->getContentSize().width*0.5,score_sprite->getContentSize().height*0.25));

	//add hisocre
	auto hiscore_sprite = Sprite::createWithSpriteFrameName("bg_score_max.png");
	addChild(hiscore_sprite);
	hiscore_sprite->setTag(TAG_HISCORE_BG);
	hiscore_sprite->setAnchorPoint(Vec2(0, 1));
	hiscore_sprite->setPosition(Vec2(450, mysize.height - 20));


	auto label_hi = Label::createWithSystemFont(String::createWithFormat("%d",hiScore)->getCString(),"Consolas",50);
	hiscore_sprite->addChild(label_hi);
	label_hi->setTag(TAG_HISCORE);
	label_hi->setPosition(Vec2(hiscore_sprite->getContentSize().width*0.5, hiscore_sprite->getContentSize().height*0.25));
	//add menu button billing button
	auto menu_frame = Sprite::createWithSpriteFrameName("btn_menu.png");
	auto bill_frame = Sprite::createWithSpriteFrameName("btn_billboard.png");

	auto menu_item = MenuItemSprite::create(menu_frame,menu_frame,CC_CALLBACK_1(UILayer::menuCallBack,this));
	auto bill_item = MenuItemSprite::create(bill_frame, bill_frame, CC_CALLBACK_1(UILayer::billCallBack, this));

	menu_item->setPosition(Vec2(0,0));
	bill_item->setPosition(Vec2(210, 0));



	auto menu = Menu::create(menu_item,bill_item,nullptr);
	addChild(menu);
	menu->setAnchorPoint(Vec2(0,1));
	menu->setPosition(Vec2(320,mysize.height-180));


	
	this->scheduleUpdate();
	return true;
}
void UILayer::menuCallBack(Ref* pSender){
	log("menu called");
	CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("move.wav");
}
void UILayer::billCallBack(Ref* pSender){
	log("bill called");
	CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("move.wav");


}
void UILayer::setHiScore(int score){
	Label * label = static_cast<Label*>(this->getChildByTag(TAG_HISCORE_BG)->getChildByTag(TAG_HISCORE));
	if (label){

		label->setString(String::createWithFormat("%d",score)->getCString());
	}
}
void UILayer::setScore(int score){
	Label * label = static_cast<Label*>(this->getChildByTag(TAG_SCORE_BG)->getChildByTag(TAG_SCORE));
	if (label){

		label->setString(String::createWithFormat("%d", score)->getCString());
	}
}
int  UILayer::getScore(){


	return score;
}
void UILayer::update(float dt){
	static int count = 0;
	if (count++==60){
		setHiScore(hiScore=hiScore + 2);
		setScore(score=score + 1);
		count = 0;
	}


}